package abel.wlmr.model.unit;

import java.util.ArrayList;
import java.util.List;

import abel.wlmr.model.WlmrBasePart;
import abel.wlmr.model.WlmrBaseUnit;
import abel.wlmr.model.WlmrDefaultUnit;
import abel.wlmr.model.WlmrException;
import abel.wlmr.model.type.DataIdBody;
import io.netty.buffer.ByteBuf;

public class AfnSecondDownPart extends WlmrBasePart {

    public static final short F_CURVE = 401;
    
    // group #
    public static final short F_FROZEN_DAY = 409;
    public static final short F_FROZEN_MONTH = 410;

    public AfnSecondDownPart() {
        super();
    }

    public AfnSecondDownPart(short guessedLen) {
        super(guessedLen);
    }

    /* (non-Javadoc)
     * @see abel.wlmr.model.WlmrBasePart#appendBuf(io.netty.buffer.ByteBuf)
     */
    @Override
    public ByteBuf appendBuf(ByteBuf buf) {
        if (id.getDa().firstAddr() == 0) {
            throw new IllegalArgumentException("DA = 0 is not allowed for AFN-OD-Second");
        }

        return super.appendBuf(buf);
    }

    @Override
    public int parseBuf(ByteBuf buf) throws WlmrException {
        id = new DataIdBody();
        id.parseBuf(buf);

        short len = (short) (getGuessedLength() - 4);

        // same DA.
        List<WlmrBaseUnit> bodys = new ArrayList<>();
        unitList.add(bodys);

        for (short da : id.getDa().addrList()) {
            // DT list for one DA.
            for (short dt : id.getDt().typeList()) {

                DataIdBody outerId = new DataIdBody();
                outerId.getDa().setAddrList(da);
                outerId.getDt().setTypeList(dt);

                switch (dt) {
                case F_CURVE:

                {
                    AfnSecondF401DownUnit body = new AfnSecondF401DownUnit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D-F401.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }
                case F_FROZEN_DAY:

                {
                    AfnSecondF409DownUnit body = new AfnSecondF409DownUnit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D-F409.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }
                case F_FROZEN_MONTH:

                {
                    AfnSecondF410DownUnit body = new AfnSecondF410DownUnit(len);
                    body.setOuterId(outerId);

                    int plen = body.parseBuf(buf);
                    if (plen == -1) {
                        throw new WlmrException("not enough bytes for 0D.F410.");
                    }

                    len -= plen;
                    bodys.add(body);

                    break;
                }

                default:

                {
                    WlmrBaseUnit body = new WlmrDefaultUnit(len);
                    body.setOuterId(outerId);

                    len -= body.parseBuf(buf);
                    bodys.add(body);

                    break;
                }
                }
            }
        }

        return getGuessedLength() - len;
    }
}
